function* generator(n) {
	for (let i = 0; i < n; i++) {
		yield i;
	}
}
let g = generator(10);
console.log(g.next(),
	g.next(),
	g.return('finish'));

const obj = { 0: 1, 1: 2, length: 2 };
obj[Symbol.iterator] = function* () {
	let index = 0;
	while (index !== this.length) {
		yield this[index++];
	}
}
console.log([...obj]);

function isPrime(n) {
	for (let i = 2; i <= Math.sqrt(n); i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
}
function* primeGenerator() {
	let number = 1;
	while (true) {
		number++;
		if (isPrime(number)) {
			yield number;
		}
	}
}
g = primeGenerator();
for (let i = 0; i < 10; i++) {
	console.log(g.next());
}

// next with parameter
function* sendParam() {
	console.log('start');
	let x = yield 2;
	console.log(`x=${x}`);
	let y = yield 3;
	console.log(`y=${y}`);
	console.log(`x+y=${x + y}`);
}
const sp = sendParam();
let i = 0;
while (v = sp.next(i), !v.done) {
	console.log(v.value);
	i++;
}

// iteration
for (let i of generator(10)) {
	console.log(i);
}

// embed
function* packedGenerator(n) {
	while (true) {
		yield* generator(n);
	}
}
let count = 3;
const pg = packedGenerator(count);
for (let i = 1; i <= count * count; i++) {
	console.log(pg.next());
}
